package com.xjtu.platform.security.login;


import com.xjtu.platform.common.Result;
import com.xjtu.platform.common.TokenUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/security/login")
public class LoginAPI {

    @Resource
    private LoginService loginService;

    @PostMapping("")
    public Result Login(@RequestBody LoginDto dto){
        boolean ok = loginService.checkLogin(dto);

        if(ok){
            String role_web = loginService.findWeb(dto);

            String token = TokenUtils.loginSign(dto.getUser_id(),dto.getUser_pwd());
            return Result.success(role_web,(Object)token);
        }

        return Result.fail(Result.ERR_CODE_BUSINESS,"账号或者密码错误");
    }

    @PostMapping("/checkUser")
    public Result checkUser(@RequestBody LoginDto dto){
        boolean ok = loginService.checkUser(dto);

        if(ok){


            return Result.success("成功");
        }

        return Result.fail(Result.ERR_CODE_BUSINESS,"账号不存在");
    }

}